// RecordManager.cpp: implementation of the RecordManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RecordManager.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

BaseRecordManager::BaseRecordManager()
{
}

BaseRecordManager::~BaseRecordManager()
{
	DeleteAllRecords();
}

void BaseRecordManager::AddRecord(BaseRecord * pRecord)
{
	m_Records.push_back(pRecord);
}

void BaseRecordManager::DeleteAllRecords()
{
	for (int i = 0;i < m_Records.size();i++)
	{
		BaseRecord * pRecord = m_Records[i];
		delete pRecord;
	}
	m_Records.clear();
}

void BaseRecordManager::DeleteRecord(BaseRecord * pRecord)
{
	m_Records.erase(std::remove(m_Records.begin(), m_Records.end(), pRecord), m_Records.end());
}

CTime Record::GetTime() const
{
	return CTime(m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond);
}

void Record::SetTime(const CTime& time)
{
	m_nYear = time.GetYear();
	m_nMonth = time.GetMonth();
	m_nDay = time.GetDay();
	m_nHour = time.GetHour();
	m_nMinute = time.GetMinute();
	m_nSecond = time.GetSecond();
}

RecordManager::RecordManager()
{
}

RecordManager::~RecordManager()
{
}

void RecordManager::AppendRecord(const Record& record)
{
	Record * pRecord = new Record(record);
	AddRecord(pRecord);
}

void RecordManager::SaveRecords(const CString& strFileName, bool bClear)
{
	int nNumOfRecords = m_Records.size();
	if (nNumOfRecords)
	{
		CFile * pFile = new CFile;

		if (pFile->Open(strFileName, CFile::modeWrite | CFile::shareDenyNone) || pFile->Open(strFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone))
		{
			pFile->Seek(0, CFile::end);

			for (int i = 0;i < m_Records.size();i++)
			{
				Record * pRecord = (Record *)m_Records[i];
				pFile->Write(pRecord, sizeof(Record));
			}

			pFile->Close();
		}

		delete pFile;

		if (bClear) DeleteAllRecords();
	}
}

DWORD RecordManager::GetNumOfRecordInFile(const CString& strFileName)
{
	CFile * pFile = NULL;
	ULONGLONG  nFileSize = 0;
	try
	{
		pFile = new CFile(strFileName, CFile::modeRead | CFile::shareDenyNone);
		nFileSize = pFile->GetLength();
		delete pFile;
	}
	catch(...)
	{
		TRACE0("Fail to get record file length!\n");
		if (pFile) delete pFile;
		nFileSize = 0;
	}

	return nFileSize/sizeof(Record);
}

CTime RealtimeRecord::GetTime() const
{
	return CTime(m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond);
}

void RealtimeRecord::SetTime(const CTime& time)
{
	m_nYear = time.GetYear();
	m_nMonth = time.GetMonth();
	m_nDay = time.GetDay();
	m_nHour = time.GetHour();
	m_nMinute = time.GetMinute();
	m_nSecond = time.GetSecond();
}

void RealtimeRecord::GetAsRecordItem(RecordItem& ri)
{
	ri.m_nYear = m_nYear;
	ri.m_nMonth = m_nMonth;
	ri.m_nDay = m_nDay;
	ri.m_nHour = m_nHour;
	ri.m_nMinute = m_nMinute;
	ri.m_nSecond = m_nSecond;
	ri.m_nState = m_nState;
	ri.m_fValue = m_fValue;
}

RealtimeRecordManager::RealtimeRecordManager(long nCoalMineId, const CString& strMonUnitId):
	BaseRecordManager(),
	m_nCoalMineId(nCoalMineId),
	m_strMonUnitId(strMonUnitId)
{
}

RealtimeRecordManager::~RealtimeRecordManager()
{
}

void RealtimeRecordManager::AppendRecord(const RealtimeRecord& record)
{
	RealtimeRecord * pRecord = new RealtimeRecord(record);
	AddRecord(pRecord);
}

void RealtimeRecordManager::DiscardRecordsEarlierThan(const CTime& time)
{
	std::vector<BaseRecord *>::iterator iter = m_Records.begin();
	while (iter != m_Records.end())
	{
		RealtimeRecord * pRecord = (RealtimeRecord *)*iter;
		CTime timeRecord(pRecord->m_nYear, pRecord->m_nMonth, pRecord->m_nDay, pRecord->m_nHour, pRecord->m_nMinute, pRecord->m_nSecond);
		if (time <= timeRecord) break;
		m_Records.erase(iter);
		delete pRecord;
		iter = m_Records.begin();
	}
}